package edu.psu.collegerecommendationhelper.coreservlets;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;

import edu.psu.collegerecommendationhelper.database.GenericDatabase;
import edu.psu.collegerecommendationhelper.database.MySQLDatabase;
import edu.psu.collegerecommendationhelper.model.PreferenceInfo;
import edu.psu.collegerecommendationhelper.model.UserBean;



@ManagedBean(name="modifyPreference")
public class ModifyPreferenceInfoBean {
@ManagedProperty(value="#{userBean}")
   private UserBean user;

   private String entered_location;
   private String entered_pubpri;
   private String entered_size;
   private String entered_sexratio;

   private PreferenceInfo preferenceInfo;


   public UserBean getUser() {
      return user;
   }
   public void setUser(UserBean user) {
      this.user = user;
   }

   public String getEntered_location() {
      return entered_location;
   }
   public void setEntered_location(String entered_location) {
      this.entered_location = entered_location;
   }

   public String getEntered_pubpri() {
      return entered_pubpri;
   }
   public void setEntered_pubpri(String entered_pubpri) {
      this.entered_pubpri = entered_pubpri;
   }

   public String getEntered_size() {
      return entered_size;
   }
   public void setEntered_size(String entered_size) {
      this.entered_size = entered_size;
   }

   public String getEntered_sexratio() {
      return entered_sexratio;
   }
   public void setEntered_sexratio(String entered_sexratio) {
      this.entered_sexratio = entered_sexratio;
   }

   public PreferenceInfo getPreferenceInfo() {
      return preferenceInfo;
   }
   public void setPreferenceInfo(PreferenceInfo preferenceInfo) {
      this.preferenceInfo = preferenceInfo;
   }

////////////////////////////////////////////////////////////////////////


   public String savePreferenceInfo() {
      MySQLDatabase mysqlDB = new MySQLDatabase();
      GenericDatabase genDB;
      genDB = mysqlDB;

      boolean action = false;
      preferenceInfo = user.getPreferenceInfo();

      if ((!entered_location.equals(preferenceInfo.getLocation())) &&
          (!entered_location.isEmpty())) {
    	 preferenceInfo.setLocation(entered_location);
         action = true;
      }

      if ((!entered_pubpri.equals(preferenceInfo.getPubpri())) &&
          (!entered_pubpri.isEmpty())) {
         preferenceInfo.setPubpri(entered_pubpri);
         action = true;
      }

      if ((!entered_size.equals(preferenceInfo.getSize())) &&
          (!entered_size.isEmpty())) {
         preferenceInfo.setSize(entered_size);
         action = true;
      }

      if ((!entered_sexratio.equals(preferenceInfo.getSexratio())) &&
          (!entered_sexratio.isEmpty())) {
         preferenceInfo.setSexratio(entered_sexratio);
         action = true;
      }


      if(true == action) {
         user.setPreferenceInfo(preferenceInfo);
         genDB.savePreferenceInfo(user.getUser_id(), preferenceInfo);

         return ("ModifyInfoPage");
      }
      else {
         return null;
      }
   }

}